﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevelopTool.Common;
using DevelopTool.Model.ChatModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms.ChatForms.MessageItemControls
{
    /// <summary>
    /// 消息控件
    /// </summary>
    public partial class MessageItemPanel : UIUserControl
    {
        private ChatRecord chatRecord;

        /// <summary>
        /// 会话消息对象
        /// </summary>
        public ChatRecord ChatRecord
        {
            get
            {
                return this.chatRecord;
            }
            set
            {
                this.chatRecord = value;
                SetChatRecordInfo();
            }
        }

        private MessageItemBase messageItem;

        public MessageItemPanel(ChatRecord chatRecord)
        {
            InitializeComponent();

            this.ChatRecord = chatRecord;
        }


        private void MessageItemPanel_Load(object sender,EventArgs e)
        {
            
            if(chatRecord.CreateTime.Date == DateTime.Now.Date)
            {
                this.lblTime.Text = chatRecord.CreateTime.ToString("HH:mm:ss");
            }
            else if(chatRecord.CreateTime.Date == DateTime.Now.AddDays(-1).Date)
            {
                this.lblTime.Text = "昨天 " + chatRecord.CreateTime.ToString("HH:mm:ss");
            }
            else
            {
                this.lblTime.Text = chatRecord.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            }


            if(this.chatRecord.ChatMessageType == Model.Enums.ChatMessageType.Text)//文本消息
            {
                this.messageItem = new MessageItem_Text(this.chatRecord);
            }
            else if(this.chatRecord.ChatMessageType == Model.Enums.ChatMessageType.File)//文件
            {
                this.messageItem = new MessageItem_File(this.chatRecord);
            }
            else if (this.chatRecord.ChatMessageType == Model.Enums.ChatMessageType.Image)//图片
            {
                this.messageItem = new MessageItem_Image(this.chatRecord);
            }

            this.Controls.Add(this.messageItem);

            //用显示的控件覆盖日期控件
            int indexControl1 = this.Controls.IndexOf(this.lblTime);
            this.Controls.SetChildIndex(this.messageItem,indexControl1 + 1);


            UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;
            switch(uIMenuStyle)
            {
                case UIMenuStyle.Black:

                    this.fillColor = ThemHelper.CustomBackColor_Black;

                    this.lblTime.BackColor = ThemHelper.CustomBackColor_Black;

                    this.uiAvatar.FillColor = ThemHelper.CustomBackColor_Black;
                    this.uiAvatar.BackColor = ThemHelper.CustomBackColor_Black;

                    this.messageItem.BackColor = ThemHelper.CustomBackColor_Black;

                    break;
                case UIMenuStyle.White:

                    this.fillColor = ThemHelper.CustomBackColor_White;

                    this.lblTime.BackColor = ThemHelper.CustomBackColor_White;

                    this.uiAvatar.FillColor = ThemHelper.CustomBackColor_White;
                    this.uiAvatar.BackColor = ThemHelper.CustomBackColor_White;

                    this.messageItem.BackColor = ThemHelper.CustomBackColor_White;

                    break;
            }

            if(chatRecord.SendUser.UserId == Setting.Chat.UserId)
            {
                this.uiAvatar.Dock = DockStyle.Right;
                this.uiAvatar.Symbol = 61979;
                this.uiAvatar.ForeColor = Color.Green;

                this.lblTime.TextAlign = ContentAlignment.TopRight;

                this.messageItem.RectColor = Setting.Theme.BackColor;
                this.messageItem.FillColor = Setting.Theme.BackColor;
                this.messageItem.FillColor2 = Setting.Theme.BackColor;
                this.messageItem.ForeColor = Color.Black;
            }
            else
            {
                this.uiAvatar.Dock = DockStyle.Left;
                this.uiAvatar.Symbol = 62141;
                this.uiAvatar.ForeColor = Setting.Theme.BackColor;

                this.lblTime.TextAlign = ContentAlignment.TopLeft;

                this.messageItem.RectColor = Color.LightGray;
                this.messageItem.FillColor = Color.LightGray;
                this.messageItem.FillColor2 = Color.LightGray;
                this.messageItem.ForeColor = Color.Black;
            }

            SizeChanged();
        }


        public new void SizeChanged()
        {
            if(this.messageItem == null) return;

            this.messageItem.SizeChanged();


            this.Height = this.uiAvatar.Height + this.messageItem.Height;

            if(chatRecord.SendUser.UserId == Setting.Chat.UserId)
            {
                this.messageItem.Location = new Point(this.lblTime.Width - this.messageItem.Width,this.uiAvatar.Height / 2);
            }
            else
            {
                this.messageItem.Location = new Point(this.uiAvatar.Location.X + this.uiAvatar.Width,this.uiAvatar.Height / 2);
            }

        }

        private void SetChatRecordInfo()
        {
            string r = Environment.NewLine;

            //用户信息
            string userInfo = $"{r}发送IP：{chatRecord.SendUser.IP}{r}{r}发送端口号：{chatRecord.SendUser.Port}{r}{r}接收IP：{chatRecord.ReceiverUser.IP}{r}{r}接收端口号：{chatRecord.ReceiverUser.Port}{r}{r}是否成功：{(chatRecord.SendState== ChatMessageSendState.Success ? "是" : "否")}";
            this.uiToolTip.SetToolTip(this.uiAvatar,userInfo,chatRecord.CreateTime.ToString(),361530,26,Setting.Theme.BackColor);
        }

        public void Send(Action<ChatRecord> action)
        {
            this.messageItem.Send(action);
        }

    }
}
